package com.yunhe.sms.dao.system.impl;


import com.yunhe.commens.dao.impl.BaseDaoImpl;
import com.yunhe.commens.dto.Pagedao;
import com.yunhe.commens.util.JdbcHelp;
import com.yunhe.sms.dao.system.UserDao;
import com.yunhe.sms.entity.system.User;
import com.yunhe.webmvc.annocation.ResponseJSON;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;


import java.sql.SQLException;
import java.util.List;

/**
 *
 */
public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao {
    @Override
    public void save(User user) {
        String sql= " insert into sys_user(user_name,password,role_id,phone,mail,status,created_time,deleted) values(?,?,?,?,?,?,now(),0)";
        super.cut ( sql,user.getUserName (),user.getPassword (),user.getRoleId (),user.getPhone (),user.getMail (),user.getStatus () );
    }

    @Override
    public void update(User user) {
        String sql=" update sys_user set user_name=?,password=?,role_id=?,phone=?,mail=?,status=? where id=?";
        super.cut ( sql,user.getUserName (),user.getPassword (),user.getRoleId (),user.getPhone (),user.getMail (),user.getStatus (),user.getId () );
    }

    @Override
    public void deleted(Long id) {
        String sql="update sys_user set deleted=1 where id=?";
        super.cut ( sql,id );
    }

    @Override
    public void updatepwd(String password,String name) {
        String sql="update sys_user set password=? where user_name=?";
        super.cut ( sql,password,name );
    }

    @Override
    public List<User> findList() {
        String sql=" select id,user_name userName,role_id roleId,phone,mail,status from sys_user where deleted=0 ";
        return super.quaryList ( sql );
    }

    @Override
    public User findOne(Long id) {
        String sql=" select id,user_name userName,password,role_id roleId,phone,mail,status from sys_user where deleted=0 and id=?";
        return super.quaryOne (sql,id  );
    }

    @Override
    public Pagedao<User> findPage(int offset, int size) {
        String listsql=" select u.id,u.user_name userName,u.phone,u.mail,u.status,r.name roleName from sys_user u LEFT JOIN sys_role r on  u.role_id=r.id and  r.deleted=0 and r.status=1 where u.deleted=0 ";
        String countsql=" select count(*) from sys_user where deleted=0 ";
        Pagedao<User> userPagedao = super.quaryPage ( listsql, countsql, offset, size );
        return userPagedao;
    }

    @Override
    public User login(User user) {
        String sql =" select id,user_name userName,password,role_id roleId,phone,mail,status from sys_user where deleted=0 and user_name=? and password=? ";
        QueryRunner queryRunner = new QueryRunner ( JdbcHelp.getDataSource () );
        User query = null;
        try {
            query = queryRunner.query ( sql, new BeanHandler<User> ( User.class ),user.getUserName (),user.getPassword () );
        } catch (SQLException e) {
            e.printStackTrace ();
        }
        return query;
    }

    @Override
    public User verifyname(String name) {
        String sql=" select id,user_name userName,password,role_id roleId,phone,mail,status from sys_user where deleted=0 and user_name=?";
        User user = super.quaryOne ( sql, name );
        return user;
    }
}
